//
//					Disposals.h
//

#ifndef __NEWA_DISPOSALS_H
#define __NEWA_DISPOSALS_H

#pragma message( "INCLUDING FILE: " __FILE__ )

#include "Disposal.h"
#include "WidgetElement.h"

namespace newa
{
	namespace Disposals
	{

		/************************************************************************/
		/*	WinOwner			                                                */
		/************************************************************************/
		class WinOwner : public Disposal
		{
		public:
			WinOwner( WidgetElement& we )
				: Disposal( we )
			{}

			void dispose( Disposal& d )
			{
				NativeWindow * ParentWindow = d.window();
				if( ParentWindow == 0 )
					m_win = NativeWindow::createPopup( *this /*d*/ );
				else
					m_win = NativeWindow::createChild( *this /*d*/, ParentWindow );
				//rect( linkedElem().rect() );
			}

			Point winPos() const
			{
				return Point( 0, 0 );
			}

			Point childPos() const
			{
				return m_rect.pos().fromBase( linkedElem().parent()->disposal().winPos() );
			}

			void repaint()
			{
				m_win->invalidate();
			}

			bool isWindowOwner() const
			{
				return true;
			}

			NativeWindow * window() const
			{
				return Loki::GetImpl( m_win );
			}

		private:
			NativeWindowRef	m_win;
			Rect			m_rect;

			Rect rect() const
			{
				//return m_win->rect();
				return m_rect;
			}

			void rect( const Rect& r )
			{
				m_rect = r;
				pixBufRect( r );
				//pixBufRect( Rect( 0, 0, r.width, r.height ) );
				m_win->rect( Rect( childPos(), r.size() ) );
				Rect rr = m_win->rect();
				//owner()->OnResize( rr.width, rr.height );
				EventTraits< EVRESIZE >::DispatcherType d;
				d.dispatch( linkedElem(), xloki::MakeTuple( rr.width, rr.height ) );

				WidgetElement::ChildIterator It;
				for( It = linkedElem().begin(); It != linkedElem().end(); It++ ) {
					It->rect( It->rect() );
				}
				//repaint();
			}

		};


		/************************************************************************/
		/*  WinLessee                                                */
		/************************************************************************/
		class WinLessee : public Disposal
		{
		public:
			WinLessee( WidgetElement& we )
				: Disposal( we )
			{}

			void dispose( Disposal& d )
			{
				//m_win = d.window();
				m_dsp = &d;
				//m_rect = d.rect();
				//rect( linkedElem().rect() );
			}

			Point winPos() const
			{
				return rect().pos().fromBase( m_dsp->winPos() );
			}

			Point childPos() const
			{
				return winPos();
			}

			void repaint()
			{
				window()->invalidate( Rect( winPos(), m_rect.size() ) );
			};

			bool isWindowOwner() const
			{
				return false;
			}

			NativeWindow * window() const
			{
				return m_dsp->window(); //m_win;
			}

		private:

			//NativeWindow * 	m_win;
			Disposal *		m_dsp;
			Rect 			m_rect;
			bool			m_mouseover;

			bool translateXY( int& x, int& y )
			{
				if( m_rect.contains( x, y ) ) {
					x -= m_rect.x;
					y -= m_rect.y;
					return true;
				}
				return false;
			}

			Rect rect() const
			{
				return m_rect;
			}

			void rect( const Rect& r )
			{
				m_rect = r;
				pixBufRect( r );
				//pixBufRect( Rect( winPos(), r.size() ) );
				//owner()->OnResize( r.width, r.height );
				EventTraits< EVRESIZE >::DispatcherType d;
				d.dispatch( linkedElem(), xloki::MakeTuple( r.width, r.height ) );

				WidgetElement::ChildIterator It;
				for( It = linkedElem().begin(); It != linkedElem().end(); It++ ) {
					It->rect( It->rect() );
				}

				linkedElem().parent()->disposal().repaint();
			}
		};
	}
}

#endif
